
local __paint__ = commonlib.GetPaint();
local Canvas = commonlib.inherit(commonlib.ToolBase);

local function if_else(c, a, b)
    if (c) then return a end
    return b;
end

-- 默认字体
local default_font_size = 16;
local default_font_weight = "normal";
local default_font_slant = "normal";
local default_font_family = "monospace";
local __paint_pen_color__ = 0x000000ff;
local __paint_brush_color__ = 0xffffffff;
local __paint_font_color__ = 0x000000ff;
local __paint_font_size__ = 0;

function Canvas:ctor()
    self.__pen_color__= 0xff;
    self.__brush_color__ = 0xffffffff;
    self.__font_color__ =  0xff;
    self.__font_size__, self.__font_weight__, self.__font_slant__, self.__font_family__ = default_font_size, default_font_weight, default_font_slant, default_font_family;
end

-- 设置画笔的颜色
function Canvas:SetPenColor(color)
    self.__pen_color__ = color or self.__pen_color__;
    if (self.__pen_color__ == __paint_pen_color__) then return end
    __paint_pen_color__ = self.__pen_color__;
    __paint__.SetPenColor(__paint_pen_color__);
end

-- 设置画刷的字符串颜色
function Canvas:SetBrushColor(color)
    self.__brush_color__ = color or self.__brush_color__;
    if (self.__brush_color__ == __paint_brush_color__) then return end 
    __paint_brush_color__ = self.__brush_color__;
    __paint__.SetBrushColor(__paint_brush_color__);
end

-- 设置画笔宽度
function Canvas:SetLineWidth(width)
    __paint__.SetLineWidth(width or 1);
end

-- 绘制点
function Canvas:DrawPoint(x, y)
    __paint__.DrawPoint(x, y);
end

-- 绘制线条
function Canvas:DrawLine(x1, y1, x2, y2, stroke)
    __paint__.DrawLine(x1, y1, x2, y2, if_else(stroke == nil, true, stroke));
end

-- 绘制三角形
function Canvas:DrawTriangle(x1, y1, x2, y2, x3, y3, fill, stroke)
    __paint__.DrawTriangle(x1, y1, x2, y2, x3, y3, if_else(fill == nil, true, fill), if_else(stroke == nil, false, stroke));
end

-- 绘制三角形
function Canvas:DrawCurve(x1, y1, x2, y2, x3, y3, fill, stroke)
    __paint__.DrawCurve(x1, y1, x2, y2, x3, y3, if_else(fill == nil, true, fill), if_else(stroke == nil, false, stroke));
end

-- 绘制矩形
function Canvas:DrawRectangle(x, y, w, h, fill, stroke)
    __paint__.DrawRectangle(x, y, w, h, if_else(fill == nil, true, fill), if_else(stroke == nil, false, stroke));
end

-- 绘制多边形
function Canvas:DrawPolygon(points, fill, stroke)
    local __points__, index = {}, 0;
    for _, point in ipairs(points) do
        __points__[index + 1] = point[1] or point.x or 0;
        __points__[index + 2] = point[2] or point.y or 0;
        index = index + 2;
    end
    __paint__.DrawPolygon(__points__, if_else(fill == nil, true, fill), if_else(stroke == nil, false, stroke));
end

-- 绘制弧线
function Canvas:DrawArc(x, y, radius, start_angle, end_angle, fill, stroke)
    __paint__.DrawArc(x, y, radius, start_angle or 0, end_angle or 360, if_else(fill == nil, true, fill), if_else(stroke == nil, false, stroke));
end

-- 绘制圆
function Canvas:DrawCircle(x, y, radius, fill, stroke)
    self:DrawArc(x, y, radius, 0, 360, fill, stroke);
end

-- 绘制九宫图
function Canvas:DrawNineImage(x, y, w, h, filepath, l, t, r, b)
    l, t, r, b = l or 0, t or 0, r or 0, b or 0;
    if (l == 0 and t == 0 and b == 0 and l == 0) then return self:DrawImage(x, y, w, h, filepath) end 
    return __paint__.DrawNineImage(x, y, w, h, filepath, l, t, r, b);
end

-- 绘制图片
function Canvas:DrawImage(x, y, w, h, filepath, is_scale)
    __paint__.DrawImage(x, y, w, h, filepath, if_else(is_scale == nil, true, is_scale));
end

-- 绘制文本
function Canvas:DrawText(x, y, w, h, text, x_align, y_align, is_clip)
    -- x 轴默认左对齐
    if (x_align == "center") then x_align = 1
    elseif (x_align == "right") then x_align = 2
    else x_align = 0 end
    -- y 轴默认居中
    if (not y_align or y_align == "center") then y_align = 1
    elseif (y_align == "right") then y_align = 2
    else y_align = 0 end
    -- 是否裁剪
    if (is_clip) then is_clip = true 
    else is_clip = false end
    -- 绘制
    __paint__.DrawText(x, y - 1, w, h, text, x_align, y_align, is_clip);
end

function Canvas:GetTextWidth(text, font_size)
    return __paint__.GetTextWidth(text, font_size or self.__font_size__ or default_font_size);
end

-- 获取指定宽度文本
function Canvas:GetTextByWidth(text, max_width, font_size)
    if(not text or text== "") then return "", "" end
    if (not max_width) then return text, "" end 

    local width = self:GetTextWidth(text, font_size);
    if(width <= max_width) then return text, "" end

    local nSize = commonlib.utf8_len(text);
    local iStart, iEnd, iMid = 1, nSize, nSize;

    while (iStart <= iEnd) do
        width = self:GetTextWidth(commonlib.utf8_sub(text, 1, iMid), font_size);
        if(width > max_width) then
            iEnd = iMid - 1;
        else
            iStart = iMid + 1;
        end
        iMid = math.floor((iStart + iEnd) / 2);
    end
    
    return commonlib.utf8_sub(text, 1, iMid), commonlib.utf8_sub(text, iMid + 1);
end

-- 设置字体
function Canvas:SetFont(weight, slant, family)
    self.__font_weight__ = weight or self.__font_weight__;
    self.__font_slant__ = slant or self.__font_slant__;
    self.__font_family__ = family or self.__font_family__;
    -- __paint__.SetFont(self.__font_weight__, self.__font_slant__, self.__font_family__);
end

-- 获取字体大小
function Canvas:GetFont()
    return self.__font_weight__, self.__font_slant__, self.__font_family__;
end

-- 设置字体大小
function Canvas:SetFontSize(font_size)
    self.__font_size__ = font_size or self.__font_size__;
    if (self.__font_size__ == __paint_font_size__) then return end 
    __paint_font_size__ = self.__font_size__;
    __paint__.SetFontSize(__paint_font_size__);
end

-- 获取字体大小
function Canvas:GetFontSize()
    return self.__font_size__;
end

-- 设置字体字符串颜色
function Canvas:SetFontStringColor(color)
    self:SetFontColor(tonumber(color));
end

-- 设置字体颜色
function Canvas:SetFontColor(color)
    self.__font_color__ = color or self.__font_color__;
    if (__paint_font_color__ == self.__font_color__) then return end 
    __paint_font_color__ = self.__font_color__;
    __paint__.SetFontColor(__paint_font_color__);
end

-- 获取字体颜色
function Canvas:GetFontColor()
    return self.__font_color__;
end

-- 平移
function Canvas:Translate(tx, ty)
    __paint__.Translate(tx or 0, ty or 0);
end

-- 缩放
function Canvas:Scale(sx, sy)
    __paint__.Scale(sx or 1, sy or 1);
end

-- 旋转
function Canvas:Rotate(angle)
    __paint__.Rotate(angle or 0);
end

-- 裁剪
function Canvas:Clip()
    __paint__.Clip();
end

-- 保存
function Canvas:Save()
    __paint__.Save();
end

-- 恢复
function Canvas:Restore()
    __paint__.Restore();
end

-- 清除
function Canvas:Clear()
    __paint__.Clear();
end

-- 冲刷
function Canvas:Flush()
    __paint__.Flush();
end

-- 绘制
function Canvas:Stroke()
    __paint__.Stroke();
end

-- 填充
function Canvas:Fill()
    __paint__.Fill();
end

-- 设置起点
function Canvas:MoveTo(x, y)
    __paint__.MoveTo(x, y);
end

-- 设置连接点
function Canvas:LineTo(x, y)
    __paint__.LineTo(x, y);
end

return Canvas;